iT邦幫忙

2021 iThome 鐵人賽

DAY 8
0
自我挑戰組

冒險村-30 Day Ruby on Rails Tips Challenge系列 第 8

冒險村08 - Preitter output in rails console

  • 分享至 

  • xImage
  •  

08 - Preitter output in rails console

Rails 的 default console 顯示看起來有點不好閱讀、不好看、也不美麗,在開發上對於資料的顯示相對於來說就是一路到底的感覺。當顯示的資料表中的欄位比較多時,對於開發來說又更不容易些,這篇將介紹一些優化 console UI 的兩個用過的 gems。

defualt console UI

  # bundle exec rails console
  User.all
  User Load (0.3ms)  SELECT "users".* FROM "users" /* loading for inspect */ LIMIT $1  [["LIMIT", 11]]
  => #<ActiveRecord::Relation [#<User id: 1, name: "chester", email: "aaa@aa.com", tel: "0123456789", created_at: "2021-08-30 05:00:22.542415000 +0000", updated_at: "2021-08-30 05:00:22.542415000 +0000">, #<User id: 2, name: "chester_2", email: "bbb@bb.com", tel: "9876543210", created_at: "2021-08-30 08:00:36.836992000 +0000", updated_at: "2021-08-30 08:00:36.836992000 +0000">, #<User id: 3, name: "chester_3", email: "ccc@cc.com", tel: "543219876540", created_at: "2021-08-30 08:00:53.073957000 +0000", updated_at: "2021-08-30 08:00:53.073957000 +0000">]>

  User.pluck(:name)
  => ["chester", "chester_2", "chester_3"]

也可以透過 yaml 格式來讓 format 更好看些

  # bundle exec rails console
  y User.first.attributes
  # or
  puts User.first.attributes.to_yaml

  User Load (0.3ms)  SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT $1  [["LIMIT", 1]]
  ---
  id: 1
  name: chester
  email: aaa@aa.com
  tel: '0123456789'
  created_at: !ruby/object:ActiveSupport::TimeWithZone
    utc: 2021-08-30 05:00:22.542415000 Z
    zone: &1 !ruby/object:ActiveSupport::TimeZone
      name: Etc/UTC
    time: 2021-08-30 05:00:22.542415000 Z
  updated_at: !ruby/object:ActiveSupport::TimeWithZone
    utc: 2021-08-30 05:00:22.542415000 Z
    zone: *1
    time: 2021-08-30 05:00:22.542415000 Z
  => nil

註: 各人覺得相較來看安裝底下兩個 gems 還是比較好看,使用經驗覺得都滿好用的,看團隊決定要安裝哪個都好~


pry-rails

gem install

  # Gemfile
  group :development, :test do
    gem "pry-rails"
  end

pry-rails console UI

  # bundle exec rails console
  User Load (0.4ms)  SELECT "users".* FROM "users"
  => [#<User:0x00007fb5a1bf1770
    id: 1,
    name: "chester",
    email: "aaa@aa.com",
    tel: "0123456789",
    created_at: Mon, 30 Aug 2021 05:00:22.542415000 UTC +00:00,
    updated_at: Mon, 30 Aug 2021 05:00:22.542415000 UTC +00:00>,
  #<User:0x00007fb5611bb188
    id: 2,
    name: "chester_2",
    email: "bbb@bb.com",
    tel: "9876543210",
    created_at: Mon, 30 Aug 2021 08:00:36.836992000 UTC +00:00,
    updated_at: Mon, 30 Aug 2021 08:00:36.836992000 UTC +00:00>,
  #<User:0x00007fb5611ba850
    id: 3,
    name: "chester_3",
    email: "ccc@cc.com",
    tel: "543219876540",
    created_at: Mon, 30 Aug 2021 08:00:53.073957000 UTC +00:00,
    updated_at: Mon, 30 Aug 2021 08:00:53.073957000 UTC +00:00>]
  
  User.pluck(:name)
  => ["chester",
 "chester_2",
 "chester_3"]

awesome_print

gem install

  # Gemfile
  group :development, :test do
    gem "awesome_print"
  end

Setting ~/.irbrc

  # ~/.irbrc

  # require
  require "awesome_print"
  # set format in rails console
  AwesomePrint.irb!

  # put hacky convenience methods here

awesome_print console UI

  # bundle exec rails console
  User.all
  User Load (0.4ms)  SELECT "users".* FROM "users"
  [
    [0] #<User:0x00007fd4c407c5b0> {
                :id => 1,
              :name => "chester",
             :email => "aaa@aa.com",
               :tel => "0123456789",
        :created_at => Mon, 30 Aug 2021 05:00:22.542415000 UTC +00:00,
        :updated_at => Mon, 30 Aug 2021 05:00:22.542415000 UTC +00:00
    },
    [1] #<User:0x00007fd4c407c4e8> {
                :id => 2,
              :name => "chester_2",
             :email => "bbb@bb.com",
               :tel => "9876543210",
        :created_at => Mon, 30 Aug 2021 08:00:36.836992000 UTC +00:00,
        :updated_at => Mon, 30 Aug 2021 08:00:36.836992000 UTC +00:00
    },
    [2] #<User:0x00007fd4c407c420> {
                :id => 3,
              :name => "chester_3",
             :email => "ccc@cc.com",
               :tel => "543219876540",
        :created_at => Mon, 30 Aug 2021 08:00:53.073957000 UTC +00:00,
        :updated_at => Mon, 30 Aug 2021 08:00:53.073957000 UTC +00:00
    }
  ]

  User.pluck(:name)
  [
    [0] "chester",
    [1] "chester_2",
    [2] "chester_3"
  ]

延伸閱讀

滿推這篇 rails console 設定來做達到更好的效果,不過有時候又覺得太麻煩了啊~還是用單一一種來處理就好,提供參考。

https://www.bruceli.net/tw/2014/08/13/awesome-rails-console-customization-using-pry.html

參考來源

My blog


上一篇
冒險村07 - Update gems & js
下一篇
冒險村09 - Time format config
系列文
冒險村-30 Day Ruby on Rails Tips Challenge30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言